package com.jiangruan.javaproject.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.MapSerializer;
import com.jiangruan.javaproject.Constants;
import com.jiangruan.javaproject.request.HttpUtils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhongjun
 * @ClassName com.jiangruan.javaproject.service.GetBsetBlockHash
 * @create 2021年12月25日 16:42
 * @desc:比特币客户端通信
 */
public class GetBsetBlockHash {
    public String getbestblockhash(){

        String auth =Constants.BITCOIN_RPC_USER+":"+Constants.BITCONIN_PWD;
        //base64编码
        Base64.Encoder encoder =Base64.getEncoder();
        String authBase64= encoder.encodeToString(auth.getBytes());

        //准备请求头
        Map<String,String>header =new HashMap<>();
        header.put("ContentType","application/json");
        header.put("Authorization","Basic "+authBase64);

        //准备请求数据
        Map<String,Object>body =new HashMap<>();
        body.put("id",1001);
        body.put("method","getbestblockhash");
        body.put("params",null);
        body.put("json_rpc","2.0");
        byte[] bodyBytes = JSONObject.toJSONBytes(body);


        String result= HttpUtils.post(Constants.BITCOIN_RPC_URL,header,bodyBytes);

        return result;
    }
}
